﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Jzy.Workflow.Code;
using System.Windows.Controls.Primitives;
using System.Windows.Browser;

namespace Jzy.Workflow
{
    public partial class Page : UserControl
    {
        private string FlowID
        {
            get
            {
                return HtmlPage.Document.QueryString["FlowID"];
            }
        }
        public Page()
        {
            
            InitializeComponent();
            if (GlobalVar.ReadOnly == false)
            {
                System.Windows.Browser.HtmlPage.Document.AttachEvent("oncontextmenu", this.OnContextMenu);
            }
            Display();
           
        }

        private void Display()
        {
            try
            {
                Flow flow = new Flow(int.Parse(FlowID), flowCanvas);
                flow.Init();
                
            }
            catch (Exception ex)
            {
                MessageBox.Show("错误：" + ex.Message);
            }
        }

        private void btnAddNode_Click(object sender, RoutedEventArgs e)
        {
            //men.Visibility = Visibility.Visible;
            
            //FlowLine line1 = new FlowLine();
            //line1.PKID = 1;

            //FlowLine line2 = new FlowLine();
            //line2.PKID = 1;            

            //AddNode node = new AddNode();
            //node.Closed += new CloseHandler(node_Closed);
            //node.HorizontalAlignment = HorizontalAlignment.Left;
            //node.VerticalAlignment = VerticalAlignment.Stretch;
            //node.Width = 400;
            //node.Height = 300;
            
            
            //LayoutRoot.Children.Add(node);

            //Popup pop = new Popup();



            //Canvas.SetZIndex(node, 5);
            //Grid.SetColumn(node, 0);
            //Grid.SetRow(node, 0);
            //Grid.SetRowSpan(node, 2);
            
        }

        //void node_Closed(object src, object args)
        //{
        //    //MessageBox.Show(args.ToString());
        //    //((AddNode)src).Visibility = Visibility.Collapsed;
        //    LayoutRoot.Children.Remove(src as UIElement);
        //    men.Visibility = Visibility.Collapsed;
        //}

        private void btnAddLine_Click(object sender, RoutedEventArgs e)
        {
            //MessageBox.Show("hello");
            //HtmlElement heScript = HtmlPage.Document.CreateElement("script");
            //heScript.SetAttribute("type", "text/javascript");
           
           
            //heScript.SetProperty("text", "function sayHello(){alert('Hello World!');}");
            //HtmlPage.Document.Body.AppendChild(heScript);
            

            //ScriptObject so = HtmlPage.Window.GetProperty("sayHello") as ScriptObject;
            ////string ss = HtmlPage.Window.Invoke("getValue","aa").ToString();

            ////MessageBox.Show(ss);
            

            //so.InvokeSelf(); 

        }

        private void btnAddNode_KeyDown(object sender, KeyEventArgs e)
        {
           
        }

        private void LayoutRoot_KeyDown(object sender, KeyEventArgs e)
        {
            
        }


        private void OnContextMenu(object sender, System.Windows.Browser.HtmlEventArgs e)
        {
            //TextField.Text = "Right Clicked Blocked at " + e.OffsetX + "," + e.OffsetY;

            //Cancels the event if it is cancelable.
            e.PreventDefault();
            FlowContextMenu menu = null;
            if (GlobalVar.ActiveElement != null)
            {
                if (GlobalVar.ActiveElement is FlowNode)
                {
                    FlowNode node = (FlowNode)GlobalVar.ActiveElement;
                    if (node.Type != NodeType.拟稿 && node.Type != NodeType.结束)
                    {
                        menu = FlowContextMenu.Create(ContextMenuType.Node, node.PKID, flowCanvas, new Point(e.OffsetX, e.OffsetY));
                        menu.Render();
                    }
                }
                else if (GlobalVar.ActiveElement is FlowLine)
                {
                    FlowLine line = (FlowLine)GlobalVar.ActiveElement;
                    menu = FlowContextMenu.Create(ContextMenuType.Line, line.PKID, flowCanvas, new Point(e.OffsetX, e.OffsetY));
                    menu.Render();
                }                
            }
            else
            {
                menu = FlowContextMenu.Create(ContextMenuType.Flow, int.Parse(FlowID), flowCanvas, new Point(e.OffsetX, e.OffsetY));
                menu.Render();
            }

            if (menu != null)
            {
                menu.Updated += new RoutedEventHandler(menu_Updated);
            }

            //if (rightclick.Text == "")
            //    rightclick.Text = "0";
            //this.rightclick.Text = (int.Parse(rightclick.Text) + 1).ToString();
            //MessageBox.Show(sender.GetType().Name);
        }

        void menu_Updated(object sender, RoutedEventArgs e)
        {           
            Display();
        }

        private void Canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            //MessageBox.Show("aaa");
            FlowContextMenu.Dispose();
        }
    }
   
}
